package com.dd.admin.business.stock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.business.product.entity.Product;
import com.dd.admin.business.product.service.ProductService;
import com.dd.admin.business.stockDetail.entity.StockDetail;
import com.dd.admin.business.stockDetail.service.StockDetailService;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.stock.entity.Stock;
import com.dd.admin.business.stock.mapper.StockMapper;
import com.dd.admin.business.stock.service.StockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.admin.common.security.SecurityUtil;
import com.dd.admin.common.security.model.JwtUser;
import com.dd.admin.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dd.admin.business.stock.domain.StockVo;
import com.dd.admin.business.stock.domain.StockDto;

import java.util.Date;
import java.util.List;

import static com.dd.admin.common.consts.MemberShopConst.STOCK_IN;
import static com.dd.admin.common.consts.MemberShopConst.STOCK_OUT;

/**
 * <p>
 * 库存单 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-27
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Autowired
    private ProductService productService;

    @Autowired
    private StockDetailService stockDetailService;

    @Override
    public IPage<StockVo> selectStockPage(StockDto stockDto) {
        Page page = PageFactory.defaultPage();
        return baseMapper.selectStockPage(page,stockDto);
    }

    @Override
    public List<StockVo> selectStockList(StockDto stockDto) {
        return baseMapper.selectStockList(stockDto);
    }

    @Override
    public Stock addStockIn(StockDto stockDto) {
        //创建入库总单
        Stock stock = BeanUtil.copyProperties(stockDto, Stock.class);
        stock.setStockTitle(DateUtil.formatChineseDate(new Date(),false,true) + SecurityUtil.getLoginUser().getDeptName() +  "入库单");
        stock.setStockNo(StringUtil.getDateStringNow());
        stock.setStockType(STOCK_IN);
        this.save(stock);

        List<Product> productList = stockDto.getProductList();
        productList.stream().forEach(product -> {
            StockDetail stockDetail = new StockDetail();
            stockDetail.setStockId(stock.getStockId());
            stockDetail.setProductId(product.getProductId());
            stockDetail.setProductName(product.getProductName());
            stockDetail.setCount(product.getCount());

            productService.addProductCount(product.getProductId(),stockDetail.getCount());
            stockDetailService.save(stockDetail);
        });
        return stock;
    }

    @Override
    public Stock addStockOut(StockDto stockDto) {
        //创建入库总单
        Stock stock = BeanUtil.copyProperties(stockDto, Stock.class);
        stock.setStockTitle(DateUtil.formatChineseDate(new Date(),false,true) + SecurityUtil.getLoginUser().getDeptName() +  "出库单");
        stock.setStockNo(StringUtil.getDateStringNow());
        stock.setStockType(STOCK_OUT);
        this.save(stock);

        List<Product> productList = stockDto.getProductList();
        productList.stream().forEach(product -> {
            StockDetail stockDetail = new StockDetail();
            stockDetail.setStockId(stock.getStockId());
            stockDetail.setProductId(product.getProductId());
            stockDetail.setProductName(product.getProductName());
            stockDetail.setCount(product.getCount());

            productService.subProductCount(product.getProductId(),stockDetail.getCount());
            stockDetailService.save(stockDetail);
        });
        return stock;
    }
}
